package com.test.demo.https;
import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;   
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  
import org.apache.http.conn.ssl.SSLContextBuilder;  
import org.apache.http.conn.ssl.TrustStrategy;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;  
    
public class HttpUtilTemp {  
  
    private static CloseableHttpClient buildSSLCloseableHttpClient()  
            throws Exception {  
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,  
                new TrustStrategy() {  
                    // 信任所有  
                    public boolean isTrusted(X509Certificate[] chain,  
                            String authType) throws CertificateException {  
                        return true;  
                    }  
                }).build();  
        // ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作，并且不会抛出javax.net.ssl.SSLException异常。  
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(  
                sslContext, new String[] { "TLSv1" }, null,  
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();  
    }  
  
    public static void main(String[] args) {  
        try {  
            CloseableHttpClient buildSSLCloseableHttpClient = buildSSLCloseableHttpClient();  
            System.setProperty("jsse.enableSNIExtension", "false");  
            HttpGet httpGet = new HttpGet(  
                    "https://115.239.138.140:8088/api/v1/query?apiun=xinchan&apiid=ZJJ001&appkey=BDBA503BE579BBD72DDF3A4C8E03E2F2&basestation=63922,183608452_55954,183608452_56481,183608452_62033,183608452_63937,183608452");  
            HttpResponse response = buildSSLCloseableHttpClient  
                    .execute(httpGet); 
            HttpEntity entity = response.getEntity();    //获得resonse 实体
            //byte[] bytes = EntityUtils.toByteArray(entity);
            //String aru  = bytes.toString();
            String aru  = EntityUtils.toString(entity);
            System.out.println(aru);
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
  
} 